package com.admission.service;

import java.io.File;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.admission.dao.SpecialArticleDao;
import com.admission.dao.SpecialAttachmentDao;
import com.admission.dao.SpecialDao;
import com.admission.dao.SpecialSiteDao;
import com.admission.model.ArrayInfo;
import com.admission.model.Special;
import com.admission.model.SpecialArticle;
import com.admission.model.SpecialAttachment;
import com.admission.model.SpecialSite;
import com.admission.util.DeleteFile;
import com.admission.util.XmlUtils;

@Repository
@Transactional
public class SpecialManager {
	private SpecialDao specialDao;
	private SpecialArticleDao specialArticleDao;
	private ArrayInfoManager arrayInfoManager;
	private SpecialAttachmentDao specialAttachmentDao;
	private SpecialSiteDao specialSiteDao;

	/**
	 * 通过id找唯一对象，无返回null
	 * 
	 * @param id
	 * @return
	 */
	public Special getSpecialById(String id) {
		return specialDao.findUniqueBy("id", id);
	}

	/**
	 * 创建主页xml
	 */
	public void createXml() {
		Page<Special> specialPage = new Page<Special>(3);
		// 专题页面
		List<PropertyFilter> filtersSpecial = PropertyFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());
		PropertyFilter pSpecial = new PropertyFilter("EQS_home",
				Special.HOMETRUE);
		filtersSpecial.add(pSpecial);
		PropertyFilter pSpecial1 = new PropertyFilter("EQS_consult",
				Special.CONSULTTRUE);
		filtersSpecial.add(pSpecial1);
		specialPage.setOrder(Page.DESC);
		specialPage.setOrderBy("inputTime");
		getPage(specialPage, filtersSpecial);
		String path = ServletActionContext.getServletContext().getRealPath(
				"/WEB-INF");
		path += "/other/mainpage-special.xml";
		XmlUtils.create(specialPage.getResult(), path);
	}

	/**
	 * 保存
	 * 
	 * @param Entity
	 * @return
	 */
	public void save(Special entity, String photoUrl) throws Exception {
		String serverrealPath = ServletActionContext.getServletContext()
				.getRealPath("/upload");
		if (photoUrl != null) {
			File file = new File(serverrealPath + File.separator
					+ entity.getPhotoUrl());
			DeleteFile.deleteByFile(file);// 删除源文件
			entity.setPhotoUrl(photoUrl);
		}
		if (StringUtils.isEmpty(entity.getId())) {
			entity.setConsult("1");
		}
		specialDao.save(entity);
	}

	/**
	 * 批量删除
	 */
	public void delete(String ids) {
		for (String id : ids.split(",")) {
			List<SpecialArticle> a = specialArticleDao.findBy("special.id", id);
			List<SpecialSite> s = specialSiteDao.findBy("special.id", id);
			List<SpecialAttachment> t = specialAttachmentDao.findBy(
					"special.id", id);
			for (SpecialArticle ar : a)
				specialArticleDao.delete(ar);
			for (SpecialSite ss : s)
				specialSiteDao.delete(ss);
			for (SpecialAttachment sa : t)
				specialAttachmentDao.delete(sa);
			specialDao.delete(id);
		}
	}

	/**
	 * 通过filter获取list
	 * 
	 * @param filters
	 * @return
	 */
	public List<Special> getListByFilter(List<PropertyFilter> filters) {
		return specialDao.find(filters);
	}

	/**
	 * 通过filter分页查询
	 * 
	 * @param filters
	 * @return
	 */
	public Page<Special> getPage(Page<Special> page,
			List<PropertyFilter> filters) {
		return specialDao.findPage(page, filters);
	}

	/**
	 * 改变状态
	 * 
	 * @param id
	 * @param status
	 */
	public void changeConsult(String status) {
		arrayInfoManager.changeConsult(status, ArrayInfo.SPECIALCONSULT);
		List<Special> l = this.getAll();
		for (Special s : l)
			s.setConsult(status);
	}

	/**
	 * 判断权限
	 * 
	 * @return
	 */
	public String checkConsult() {
		return arrayInfoManager.checkConsult(ArrayInfo.SPECIALCONSULT);
	}

	public List<Special> getAll() {
		return specialDao.getAll();
	}

	public SpecialDao getSpecialDao() {
		return specialDao;
	}

	@Autowired
	public void setSpecialDao(SpecialDao specialDao) {
		this.specialDao = specialDao;
	}

	public ArrayInfoManager getArrayInfoManager() {
		return arrayInfoManager;
	}

	@Autowired
	public void setArrayInfoManager(ArrayInfoManager arrayInfoManager) {
		this.arrayInfoManager = arrayInfoManager;
	}

	public SpecialArticleDao getSpecialArticleDao() {
		return specialArticleDao;
	}

	@Autowired
	public void setSpecialArticleDao(SpecialArticleDao specialArticleDao) {
		this.specialArticleDao = specialArticleDao;
	}

	public SpecialAttachmentDao getSpecialAttachmentDao() {
		return specialAttachmentDao;
	}

	@Autowired
	public void setSpecialAttachmentDao(
			SpecialAttachmentDao specialAttachmentDao) {
		this.specialAttachmentDao = specialAttachmentDao;
	}

	public SpecialSiteDao getSpecialSiteDao() {
		return specialSiteDao;
	}

	@Autowired
	public void setSpecialSiteDao(SpecialSiteDao specialSiteDao) {
		this.specialSiteDao = specialSiteDao;
	}

}
